package com.example.site.service.impl;

import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.site.entity.Room;
import com.example.site.entity.RoomJoinType;
import com.example.site.entity.Type;
import com.example.site.service.RoomService;
import com.example.site.mapper.RoomMapper;
import com.example.site.vo.PageVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author haoka
 * @description 针对表【hotel_room】的数据库操作Service实现
 * @createDate 2023-04-11 20:46:21
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room>
        implements RoomService {
    @Resource
    RoomMapper roomMapper;

    /**
     * @param
     * @return
     * @deprecated 创建房间
     */
    public SaResult create(Room room) {
        if (StringUtils.isBlank(room.getNumber())) {
            return SaResult.error("编号不能为空");
        } else {
            boolean result = this.save(room);
            if (result) {
                return SaResult.ok();
            } else {
                return SaResult.error("创建失败");
            }
        }
    }

    /**
     * @param
     * @return
     * @deprecated 房间查询
     */
    public SaResult getList(Map<String, String> data) {
        String number = data.get("number");
        String _page = data.getOrDefault("page", "1");
        String _page_size = data.getOrDefault("page_size", "20");
        Page<Room> page = new Page<>(Integer.parseInt(_page), Integer.parseInt(_page_size));

        LambdaQueryWrapper<Room> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(number)) {
            queryWrapper.like(Room::getNumber, number);
        }

        this.page(page, queryWrapper);
        PageVO<Room> pageVO = new PageVO<>(page);
        return SaResult.data(pageVO);
    }

    /**
     * @param
     * @return
     * @deprecated 房间查询携带类型信息
     */
    public SaResult getListJoinType(Map<String, String> data) {
        String keyword = data.get("keyword");
        String _page = data.getOrDefault("page", "1");
        String _page_size = data.getOrDefault("page_size", "20");
        Page<RoomJoinType> page = new Page<>(Integer.parseInt(_page), Integer.parseInt(_page_size));

        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like("number", keyword);
        }

        roomMapper.selectPageRoomJoinType(page, queryWrapper);
        PageVO<RoomJoinType> pageVO = new PageVO<>(page);
        return SaResult.data(pageVO);
    }

    /**
     * @param
     * @return
     * @deprecated 编辑
     */
    public SaResult edit(Room room) {
        boolean result = this.updateById(room);
        if (result) {
            return SaResult.ok();
        } else {
            return SaResult.error("修改失败");
        }
    }
}




